package com.prp.gwt.client.admin;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.widgets.admin.AccountManagement;
import com.prp.gwt.client.widgets.admin.AdminView;
import com.prp.gwt.client.widgets.admin.CommunityMember;

public class AdminHistoryHandler implements ValueChangeHandler<String> {

	public static final String ADMINLIST = "adminlist";
	public static final String ROOTLIST = "rootlist";
	public static final String ACCOUNTMANAGEMENT = "accountmanagement";
	public static final String COMMUNITYLIST = "communitylist";
	public static final String USERQUERY = "userquery";
	public static final String ADDUSER = "adduser";
	public static final String ACTIVATEUSER = "activateuser";
	public static final String COMMUNITY = "community=";
	public static final String UNDIVIDED = "undivided";
	public static final String SUBJECT = "subject";

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String history = event.getValue();
		if(history.equals("")){
			//do nothing
		}
		else if (history.equals(ADMINLIST)) {
			AdminListener.getInstance().updateAdminList();
			AdminView.getInstance().showWidget(AdminView.ADMINLIST);
		} else if (history.equals(ROOTLIST)) {
			AdminListener.getInstance().updateRootList();
			AdminView.getInstance().showWidget(AdminView.ROOTLIST);
		} else if (history.equals(ACCOUNTMANAGEMENT)) {
			AdminView.getInstance().showWidget(AdminView.ACCOUNTMANAGEMENT);
			AccountManagement.getInstance().showWidget(
					AccountManagement.QUERY_PANEL);
		} else if (history.equals(COMMUNITYLIST)) {
			AdminListener.getInstance().updateCommunityList();
			AdminView.getInstance().showWidget(AdminView.COMMUNITYLIST);
		} else if (history.equals(USERQUERY)) {
			AdminView.getInstance().showWidget(AdminView.ACCOUNTMANAGEMENT);
			AccountManagement.getInstance().showWidget(
					AccountManagement.QUERY_PANEL);
		} else if (history.equals(ACTIVATEUSER)) {
			AdminView.getInstance().showWidget(AdminView.ACCOUNTMANAGEMENT);
			AccountManagement.getInstance().showWidget(
					AccountManagement.INACTIVATED_USER_PANEL);
			AdminListener.getInstance().getInactivatedUserList();
		} else if (history.startsWith(COMMUNITY)) {
			String token = history.substring(COMMUNITY.length());
			if (token.equals(UNDIVIDED)) {
				CommunityMember.getInstance().reset();
				AdminView.getInstance().showWidget(AdminView.COMMUNITYMEMBER);
				AdminListener.getInstance().getCommunityMemeber(-1);
			} else {
				try {
					int id = Integer.parseInt(token);
					if (id < 0)
						Window.Location.replace(Host.getPAGE_NOT_FOUND());
					CommunityMember.getInstance().reset();
					AdminView.getInstance().showWidget(4);
					AdminListener.getInstance().getCommunityMemeber(id);
				} catch (NumberFormatException e) {
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				}
			}
		}
		else
			Window.Location.replace(Host.getPAGE_NOT_FOUND());
	}

}
